package com.shuorikeji.huohuo.ui.sj;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.AddMhAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.MhSjMainBean;
import com.shuorikeji.huohuo.bean.MhSjSSubBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class AddMhActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlView;
    @BindView(R.id.rv_data)
    RecyclerView rvData;

    private int curPage;
    private List<MhSjSSubBean> mhSjList = new ArrayList<>();
    private AddMhAdapter addMhAdapter;
    private int SD_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_add_mh);
    }

    @Override
    protected void initData() {
        initSmartRefreshLayout();
        initAdapter();
        initIntent();
        curPage = 1;
        getMhSjList();
        ivBack.setOnClickListener(view -> {
            setResult(RESULT_OK);
            finish();
        });
    }

    private void initSmartRefreshLayout() {
        srlView.setRefreshHeader(new ClassicsHeader(AddMhActivity.this));
        srlView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPage = 1;
                getMhSjList();
            }
        });
        srlView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPage = curPage + 1;
                getMhSjList();
            }
        });
    }

    private void initAdapter() {
        rvData.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new SpacesItemDecoration(0, 0, 15, 0));
        addMhAdapter = new AddMhAdapter(AddMhActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhSjSSubBean data = (MhSjSSubBean) object;
                        addMh(data.getCartoon_id());
                        break;
                }
            }
        });
        rvData.setAdapter(addMhAdapter);
    }

    private void initIntent() {
        Intent intent = getIntent();
        SD_ID = intent.getIntExtra("SD_ID", -1);
    }

    private void getMhSjList() {
        mPresenter.mhSjList(true, 20, curPage, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                srlView.finishRefresh();
                srlView.finishLoadMore();
                switch (flag) {
                    case 1:
                        MhSjMainBean data = (MhSjMainBean) object;
                        if (curPage == 1) {
                            mhSjList.clear();
                        }
                        mhSjList.addAll(data.getData().getData());
                        addMhAdapter.setData(mhSjList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(AddMhActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void addMh(int cartoonId) {
        mPresenter.addMhToSd(true, SD_ID, cartoonId, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        ToastShowUtil.showToastCenter(AddMhActivity.this, "已添加");
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(AddMhActivity.this, msg);
                        break;
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            setResult(RESULT_OK);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}